#include"contact_obs.h"

void ShowMenu()//显示目录
{
    cout<<"***********************"<<endl;
    cout<<"*****1、添加联系人*****"<<endl;
    cout<<"*****2、显示联系人*****"<<endl;
    cout<<"*****3、删除联系人*****"<<endl;
    cout<<"*****4、查找联系人*****"<<endl;
    cout<<"*****5、修改联系人*****"<<endl;
    cout<<"*****6、清空联系人*****"<<endl;
    cout<<"*****0、退出通讯录*****"<<endl;
    cout<<"***********************"<<endl;
}
void Add_person(Address_Book* abs)//添加联系人
{
    if(abs->size == MAX)
    {
        cout<<"通讯录已满，添加失败"<<endl;
        return;
    }
    else
    {
        string name;
        cout<<"请输入姓名："<<endl;
        cin>>name;
        abs->Array[abs->size].name = name;
        cout<<"请输入性别：(1-男，2-女)"<<endl;
        int gender;//1-男，2-女
        cin>>gender;
        abs->Array[abs->size].gender = gender;
        cout<<"请输入年龄："<<endl;
        int age;
        cin>>age;
        abs->Array[abs->size].age = age;
        cout<<"请输入电话："<<endl;
        string Contact_Phone;
        cin>>Contact_Phone;
        abs->Array[abs->size].Contact_Phone = Contact_Phone;
        cout<<"请输入住址："<<endl;
        string Address;
        cin>>Address;
        abs->Array[abs->size].Address = Address;
        //更新通讯录人数
        abs->size++;
        cout<<"添加成功"<<endl;
        cout<<"任意键继续......"<<endl;
        _getch();
    }
}
void Show_Person(Address_Book* abs)//显示所有联系人
{
    if(abs->size == 0)
    {
        cout<<"记录为空"<<endl;
    }
    else
    {
        for(int i;i<abs->size;i++)
        {
            cout<<"姓名："<<abs->Array[i].name<<"\t";
            cout<<"性别："<<(abs->Array[i].gender == 1 ? "男" : "女")<<"\t";
            cout<<"年龄："<<abs->Array[i].age<<"\t";
            cout<<"电话："<<abs->Array[i].Contact_Phone<<"\t";
            cout<<"住址："<<abs->Array[i].Address<<endl; ;
        }
    }
    cout<<"任意键继续......"<<endl;
    _getch(); 
}
int isExist(Address_Book *abs,string name)//判断是否存在
{
    for(int i = 0;i<abs->size;i++)
    {
        if (abs->Array[i].name == name)
        {
            return i;
        }
    }
    return -1;
}
void DeletePerson(Address_Book *abs)//删除联系人
{
    cout<<"请输入要删除的联系人姓名"<<endl;
    string name;
    cin>>name;
    int ret = isExist(abs,name);
    if(ret!=-1)
    {
        for (int  i = ret;i<abs->size-1;i++)
        {
            abs->Array[i] = abs->Array[i+1];
        }
        abs->size--;
        cout<<"删除成功"<<endl;
    }
    else
    {
        cout<<"查无此人"<<endl;
    }
    cout<<"任意键继续......"<<endl;
    _getch();
}
void FindPerson(Address_Book *abs)//查找联系人
{
    cout<<"请输入查找联系人姓名"<<endl;
    string name;
    cin>>name;
    int ret = isExist(abs,name);
    if (ret !=-1)
    {
        cout<<"姓名："<<abs->Array[ret].name<<"\t";
        cout<<"性别："<<(abs->Array[ret].gender == 1 ? "男" : "女")<<"\t";
        cout<<"年龄："<<abs->Array[ret].age<<"\t";
        cout<<"电话："<<abs->Array[ret].Contact_Phone<<"\t";
        cout<<"住址："<<abs->Array[ret].Address<<endl; ;
    }
    else
    {
        cout<<"查无此人"<<endl;
    }
    cout<<"任意键继续......"<<endl;
    _getch();
}
void ModifyPerson(Address_Book *abs)//修改联系人
{
    cout<<"请输入要修改的联系人姓名"<<endl;
    string name;
    cin>>name;
    int ret  = isExist(abs,name);
    if(ret!=-1)
    {
        string name;
        cout<<"请输入姓名："<<endl;
        cin>>name;
        abs->Array[ret].name = name;
        cout<<"请输入性别：(1-男，2-女)"<<endl;
        int gender;//1-男，2-女
        cin>>gender;
        abs->Array[ret].gender = gender;
        cout<<"请输入年龄："<<endl;
        int age;
        cin>>age;
        abs->Array[ret].age = age;
        cout<<"请输入电话："<<endl;
        string Contact_Phone;
        cin>>Contact_Phone;
        abs->Array[ret].Contact_Phone = Contact_Phone;
        cout<<"请输入住址："<<endl;
        string Address;
        cin>>Address;
        abs->Array[ret].Address = Address;
        cout<<"修改成功!"<<endl;
    }
    else
    {
        cout<<"查无此人"<<endl;
    }
    cout<<"任意键继续......"<<endl;
    _getch();
}
void ClearPerson(Address_Book *abs)//清空联系人
{
    cout<<"清除成功!\t";
    abs->size =0;
    cout<<"任意键继续......"<<endl;
    _getch();
}